package com.lantosec.hmrp.modules.form.controller;


import com.lantosec.hmrp.core.kit.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.form.entity.FormTable;
import com.lantosec.hmrp.modules.form.service.IFormTableService;
import com.lantosec.hmrp.modules.form.vo.FormTableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 自定义表单表定义 前端控制器
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-15
 */
@Api(value = "自定义表单表定义控制器", tags = "自定义表单表定义控制器")
@RestController
@RequestMapping("/form/table/")
public class FormTableController extends BaseController {
    @Autowired
    private IFormTableService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<FormTableVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<FormTableVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<FormTable> page, FormTable condition){
        Page<FormTableVO> result = service.page(page, condition);
        return Result.ok(result);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<FormTableVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "自定义表单新增数据", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "自定义表单布局VO对象", required = true) @RequestBody @Validated FormTableVO vo) {
        vo.setCreateUserId(super.getUserId());
        vo.setCreateUserName(super.getName());
        return service.save(vo);
    }

    @PutMapping("update")
    @ControllerLog(name = "自定义表单修改数据", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "自定义表单布局VO对象", required = true) @RequestBody FormTableVO vo){
        FormTable entity = vo.convertToEntity();
        entity.setCreateUserId(super.getUserId());
        entity.setCreateUserName(super.getName());
        return service.update(entity);
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "自定义表单布局删除数据", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("queryPrimary")
    @ApiOperation(value = "查询主表")
    public Result<List<FormTableVO>> queryPrimary(@ApiParam(value = "数据库名称") String tableSchema){
        return Result.ok(entity2VO(service.queryPrimary(tableSchema)));
    }

    @GetMapping("querySublist/{parentId}")
    @ApiOperation(value = "根据父表Id查询子表")
    public Result<List<FormTableVO>> querySublist(@ApiParam(value = "父表Id", required = true) @PathVariable(name="parentId") String parentId){
        return Result.ok(entity2VO(service.querySublist(parentId)));
    }
}
